local body_transformer = require "kong.plugins.body-error.body"
local find = string.find
local lower = string.lower
local kong = kong

local function is_json_body(content_type)
  return content_type and find(lower(content_type), "application/json", nil, true)
end


local ResponseErrorHandler = {
  PRIORITY = 800,
  VERSION = "2.0.2",
}

function ResponseErrorHandler:body_filter(conf)
  if not is_json_body(kong.response.get_header("Content-Type")) then
    return
  end

  local body = kong.response.get_raw_body()
  if body then
    return kong.response.set_raw_body(body_transformer.transform_json_body(body))
  end
end


return ResponseErrorHandler
